﻿// Copyright (c) 2009 Kjetil Eraker Samuelsen
// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
// All other rights reserved.

using System;
using Microsoft.Win32;

namespace Ksamuelsen.Office.Outlook.Addin.VDialer {
    internal class VDialerJournalOptions {
        private bool             _useDefaultFolder;
        private OutlookFolderRef _folder;
        private string           _subject;
        private string           _entryType;
        private string           _category;
        private bool             _linkContact;
        private bool             _copyCompany;
        private bool             _markPrivate;
        private bool             _autosave;
        private bool             _openForm;

        internal VDialerJournalOptions() {
            string strfolder;
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(Properties.Resources.REG_KEY_JOURNAL)) {
                _useDefaultFolder = VDialerConfiguration.GetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_DEFAULT_FOLDER);
                strfolder         = VDialerConfiguration.GetRegistryStringValue(key, Properties.Resources.REG_KEY_JOURNAL_FOLDER);
                _subject          = VDialerConfiguration.GetRegistryStringValue(key, Properties.Resources.REG_KEY_JOURNAL_SUBJECT);
                _entryType        = VDialerConfiguration.GetRegistryStringValue(key, Properties.Resources.REG_KEY_JOURNAL_ENTRY_TYPE);
                _category         = VDialerConfiguration.GetRegistryStringValue(key, Properties.Resources.REG_KEY_JOURNAL_CATEGORY);
                _linkContact      = VDialerConfiguration.GetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_LINK_CONTACT);
                _copyCompany      = VDialerConfiguration.GetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_COPY_COMPANY);
                _markPrivate      = VDialerConfiguration.GetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_MARK_PRIVATE);
                _autosave         = VDialerConfiguration.GetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_AUTOSAVE);
                _openForm         = VDialerConfiguration.GetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_OPENFORM);

                if (key != null)
                    key.Close();
            }
            if (String.IsNullOrEmpty(strfolder) || _useDefaultFolder)
                _folder = null;
            else
                _folder = new OutlookFolderRef(strfolder);

        }

        public bool UseDefaultFolder {
            get { return _useDefaultFolder; }
            set { _useDefaultFolder = value; }
        }

        public OutlookFolderRef Folder {
            get { return ((_useDefaultFolder || (_folder == null)) ? OutlookManager.GetDefaultJournalFolder() : _folder); }
            set { _folder = value; }
        }

        public string Subject {
            get { return _subject; }
            set { _subject = value; }
        }

        public string EntryType {
            get { return _entryType; }
            set { _entryType = value; }
        }

        public string Category {
            get { return _category; }
            set { _category = value; }
        }

        public bool LinkContact {
            get { return _linkContact; }
            set { _linkContact = value; }
        }

        public bool CopyCompany {
            get { return _copyCompany; }
            set { _copyCompany = value; }
        }

        public bool MarkPrivate {
            get { return _markPrivate; }
            set { _markPrivate = value; }
        }

        public bool AutoSave {
            get { return (!_openForm) ? true : _autosave; }
            set { _autosave = value; }
        }

        public bool OpenForm {
            get { return _openForm; }
            set { _openForm = value; }
        }

        public void Save() {
            using (RegistryKey key = VDialerConfiguration.GetOrCreateKey(Properties.Resources.REG_KEY_JOURNAL)) {
                string strfolder = ((_folder == null) || (_useDefaultFolder)) ? String.Empty : _folder.Id;


                VDialerConfiguration.SetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_DEFAULT_FOLDER, _useDefaultFolder);
                VDialerConfiguration.SetRegistryStringValue(key, Properties.Resources.REG_KEY_JOURNAL_FOLDER, strfolder);
                VDialerConfiguration.SetRegistryStringValue(key, Properties.Resources.REG_KEY_JOURNAL_SUBJECT, _subject);
                VDialerConfiguration.SetRegistryStringValue(key, Properties.Resources.REG_KEY_JOURNAL_ENTRY_TYPE, _entryType);
                VDialerConfiguration.SetRegistryStringValue(key, Properties.Resources.REG_KEY_JOURNAL_CATEGORY, _category);
                VDialerConfiguration.SetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_LINK_CONTACT, _linkContact);
                VDialerConfiguration.SetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_COPY_COMPANY, _copyCompany);
                VDialerConfiguration.SetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_MARK_PRIVATE, _markPrivate);
                VDialerConfiguration.SetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_OPENFORM, _openForm);
                VDialerConfiguration.SetRegistryBoolValue(key, Properties.Resources.REG_KEY_JOURNAL_AUTOSAVE, _autosave);

                key.Close();
            }

        }

           


    }
}
